package com.louis.scheduler;

import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Configurable;

import com.louis.mail.Mail;
import com.louis.scheduler.support.AbstractJobDetailAndTrigger;
import com.louis.util.JodaUtil;

import java.util.Date;

/**
 * @author Lijun jiang
 * @Date: May 10, 2011
 * @Time: 4:37:18 PM
 */

@Configurable(autowire = Autowire.BY_NAME)
public class MailSenderJobDetailAndTrigger extends AbstractJobDetailAndTrigger {

	public MailSenderJobDetailAndTrigger() {
	}

	public MailSenderJobDetailAndTrigger(Mail mail) {
		super("mailSendingJobDetail" + JodaUtil.now().getMillis(), mail.getSubject());
		addToJobDataMap("mail", mail);
	}

	public Date getExecuteDate() {
		return JodaUtil.now().plusMinutes(1).toDate();
	}

	public void doExecute(JobExecutionContext jobExecutionContext)
			throws Exception {
		JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
		Mail mail = (Mail) jobDataMap.get("mail");
		mailSender.send(mail);
	}

}